
//
//  FDMSearchDetailViewController.m
//  Entertainment
//
//  Created by dllo on 16/4/26.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMSearchDetailViewController.h"
#import "FDMSearchDetailTableViewCell.h"
#import "FDMSearchTableViewCellSectionOne.h"
#import "FDMSearchTableViewCellSectionTwo.h"
#import "FDMSearchDetailModel.h"
#import "FDMSearchDetailUserModel.h"
#import "FDMSearchTracksModel.h"

@interface FDMSearchDetailViewController ()<UITableViewDataSource, UITableViewDelegate, PlayButtonDelegate, DownLoadDelegate>

@property (nonatomic, retain) NSDictionary *albumDic;/**< 编辑字典 */

@property (nonatomic, retain) NSMutableArray *albumModelArr;/**< 专辑model数组 */

@property (nonatomic, retain) NSDictionary *userDic;/**< 用户字典 */

@property (nonatomic, retain) NSMutableArray *userModelArr;/**< 用户model数组 */

@property (nonatomic, retain) NSDictionary *trackDic;/**< 声音字典 */

@property (nonatomic, retain) NSMutableArray *trackModelArr;/**< 声音数组 */

@property (nonatomic, retain) FDMBaseTableView *tableView;

@end

@implementation FDMSearchDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden = NO;
    self.tableView = [[FDMBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 113) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[FDMSearchDetailTableViewCell class] forCellReuseIdentifier:@"FDMSearchDetailTableViewCell"];
    
    [self.tableView registerClass:[FDMSearchTableViewCellSectionOne class] forCellReuseIdentifier:@"FDMSearchTableViewCellSectionOne"];
    [self.tableView registerClass:[FDMSearchTableViewCellSectionTwo class] forCellReuseIdentifier:@"FDMSearchTableViewCellSectionTwo"];
    
    [self dataHandle];

}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"专辑";
    }
    if (section == 1) {
        return @"用户";
    } else {
        return @"声音";
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 120;
    } if (indexPath.section == 1) {
        return 100;
    } else {
        return 100;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.albumModelArr.count;
    } if (section == 1) {
        return self.userModelArr.count;
    } else {
        return self.trackModelArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        FDMSearchDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FDMSearchDetailTableViewCell"];
        FDMSearchDetailModel *model = self.albumModelArr[indexPath.row];
        cell.model = model;
        return cell;
    } if (indexPath.section == 1) {
        FDMSearchTableViewCellSectionOne *cell = [tableView dequeueReusableCellWithIdentifier:@"FDMSearchTableViewCellSectionOne"];
        FDMSearchDetailUserModel *model = self.userModelArr[indexPath.row];
        cell.model = model;
        return cell;
    } else {
        FDMSearchTableViewCellSectionTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"FDMSearchTableViewCellSectionTwo"];
        FDMSearchTracksModel *model = self.trackModelArr[indexPath.row];
        cell.model = model;
        cell.playButtonDelegate = self;
        cell.downLoadDelegate = self;
        return cell;
    }
}

#pragma mark - 下载和播放协议方法
- (void)clickDownLoadButton {
    
}

- (void)clickPlayButton {
    
}

#pragma mark - 数据处理
- (void)dataHandle {
    //    http://search.ximalaya.com/front/v1?core=all&kw=金庸&page=1&rows=3&spellchecker=true
    [APPTools GETWithURL:[NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?core=all&kw=%@&page=1&rows=3&spellchecker=true", self.titleStr] par:nil success:^(id responseObject) {
        
        self.albumDic = responseObject[@"album"];
        self.albumModelArr = [FDMSearchDetailModel baseModelHandelWithArray:self.albumDic[@"docs"]];
        self.userDic = responseObject[@"user"];
        self.userModelArr = [FDMSearchDetailUserModel baseModelHandelWithArray:self.userDic[@"docs"]];
        self.trackDic = responseObject[@"track"];
        self.trackModelArr = [FDMSearchTracksModel baseModelHandelWithArray:self.trackDic[@"docs"]];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    NSString *str = @"http://search.ximalaya.com/front/v1?condition=relation&core=album&kw=罗辑思维&page=1&rows=20&spellchecker=true";//找专辑
    NSString *str1 = @"http://search.ximalaya.com/front/v1?condition=relation&core=user&kw=罗辑思维&page=1&rows=20&spellchecker=true";//找人
    NSString *str2 = @"http://search.ximalaya.com/front/v1?condition=relation&core=track&kw=罗辑思维&page=1&rows=20&spellchecker=true";//找声音，相关度
    NSString *str3 = @"http://search.ximalaya.com/front/v1?condition=recent&core=track&kw=罗辑思维&page=1&rows=20&spellchecker=true";//找声音，最新上传
    NSString *str4 = @"http://search.ximalaya.com/front/v1?condition=play&core=track&kw=罗辑思维&page=1&rows=20&spellchecker=true";//找声音，最多播放

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
